home *** CD-ROM | disk | FTP | other *** search
/ Power DOS 1996 July / Power DOS - July 1996.iso / sound / c_labs / awe / adip.exe / ADIP01.ZIP / DOS / RX_ORIEN.C
C/C++ Source or Header  |  1995-07-24  |  4KB  |  153 lines

  1. /*
  2.  *  File:    rx_orient.c
  3.  *  Created:    4/17/95
  4.  *
  5.  *  Description:
  6.  *
  7.  *  This file contains sample code to handle receiver orientation
  8.  *  since the basic c3da core does not allow a change in receiver orientation.
  9.  *  There are two routines defined in this example:
  10.  *
  11.  *    setEmitterPosition - a replacement for c3daSetEmitterPosition
  12.  *    setReceiverOrientation - a new routine to handle receiver orientation
  13.  *
  14.  *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  15.  *  KIND,  EITHER EXPRESSED OR IMPLIED,  INCLUDING BUT NOT LIMITED TO THE
  16.  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  17.  *  PURPOSE.
  18.  *
  19.  *  Copyright (c) 1995 Creative Technology Ltd. All Rights Reserved.
  20.  *
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <math.h>
  25.  
  26. #include "ctaweapi.h"
  27.  
  28. #define    NUMBER_OF_EMITTERS    4
  29. #define    DEG2RAD            0.017453f;
  30.  
  31. /*
  32.  * example utility structs
  33.  */
  34.  
  35. typedef struct _Emitter {
  36.     int            x;    /* world x position */
  37.     int            y;    /* world y position */
  38.     int                 z;      /* world z position */
  39.     c3daEmitter        em;    /* handle to c3da core emitter     */
  40.                 /* need to be "created" using      */
  41.                 /* c3daCreateEmitter         */
  42. } Emitter;
  43.  
  44. typedef struct _Receiver {
  45.     float        a[9];    /* rotation matrix for yaw, pitch, roll */
  46.                 /* A = | a[0]  a[1]  a[2] |    */
  47.                  /*     | a[3]  a[4]  a[5] |    */
  48.                 /*     | a[6]  a[7]  a[8] |    */
  49.     c3daReceiver    rx;    /* handle to c3da core receiver */
  50.                 /* need to be "created" using    */
  51.                 /* c3daCreateReceiver        */
  52. } Receiver;
  53.  
  54. /* 
  55.  * as an example, allocate some global emitters and one receiver
  56.  */
  57.  
  58. Receiver    thisRx;
  59. Emitter        Em[NUMBER_OF_EMITTERS];
  60.  
  61. /*
  62.  * sample "world" setEmitterPosition routine
  63.  */
  64.  
  65. void
  66. setEmitterPosition( Emitter* pEm, int x, int y, int z )
  67. {
  68.  
  69.     int        new_x, new_y, new_z;
  70.  
  71.     /* 
  72.      * save the world coordinates 
  73.      */
  74.  
  75.     pEm->x = x;
  76.     pEm->y = y;
  77.     pEm->z = z;
  78.  
  79.     /*
  80.      * rotate this emitter so that it is relative to the fixed receiver
  81.      * in the c3da core
  82.      */
  83.  
  84.     new_x = thisRx.a[0]*x + thisRx.a[1]*y + thisRx.a[2]*z;
  85.     new_y = thisRx.a[3]*x + thisRx.a[4]*y + thisRx.a[5]*z;
  86.     new_z = thisRx.a[6]*x + thisRx.a[7]*y + thisRx.a[8]*z;
  87.  
  88.     /*
  89.      * now tell the c3da core about the new position
  90.      */
  91.  
  92.     c3daSetEmitterPosition ( &(pEm->em), new_x, new_y, new_z );
  93.  
  94. }
  95.  
  96. /*
  97.  * sample "world" setReceiverOrientation routine
  98.  *
  99.  * Arguments:
  100.  *    yaw - rotation around the z-axis (-180 to 180 degrees)
  101.  *    pitch - rotation around the y-axis (-180 to 180 degrees)
  102.  *    roll - rotation around the x-axis (-180 to 180 degrees)
  103.  *
  104.  * Notes:
  105.  *    o  order of rotations - roll, pitch, yaw
  106.  *    o  Instead of actually changing the orientation of the receiver,
  107.  *       we equivalently move all the emitters around the fixed receiver 
  108.  *       in the c3da core.  The rotation matrix used to move the emitters
  109.  *       is simply the transpose of the "composite" rotation matrix defined
  110.  *       by yaw, pitch, roll and the order of rotations.
  111.  */
  112.  
  113. void
  114. setReceiverOrientation( int yaw, int pitch, int roll )
  115. {
  116.  
  117.     float    yaw_f;
  118.     float    pitch_f;
  119.     float    roll_f;
  120.  
  121.     /*
  122.      * convert yaw, pitch, and roll to radians
  123.      */
  124.  
  125.     yaw_f = DEG2RAD * (float) yaw;
  126.     pitch_f = DEG2RAD * (float) pitch;
  127.     roll_f = DEG2RAD * (float) roll;
  128.  
  129.     /*
  130.      * setup the inverse rotation matrix to handle yaw, pitch, and roll
  131.      */
  132.  
  133.     thisRx.a[0] = cos(yaw_f)*cos(pitch_f);
  134.     thisRx.a[1] = sin(yaw_f)*cos(pitch_f);
  135.     thisRx.a[2] = -sin(pitch_f);
  136.     thisRx.a[3] = -sin(yaw_f)*cos(roll_f) + cos(yaw_f)*sin(pitch_f)*sin(roll_f);
  137.     thisRx.a[4] = cos(yaw_f)*cos(roll_f) + sin(yaw_f)*sin(pitch_f)*sin(roll_f);
  138.     thisRx.a[5] = cos(pitch_f)*sin(roll_f);
  139.     thisRx.a[6] = sin(yaw_f)*sin(roll_f) + cos(yaw_f)*sin(pitch_f)*cos(roll_f);
  140.     thisRx.a[7] = -cos(yaw_f)*sin(roll_f) + sin(yaw_f)*sin(pitch_f)*cos(roll_f);
  141.     thisRx.a[8] = cos(pitch_f)*cos(roll_f);
  142.  
  143.     /*
  144.      * loop over the emitters letting them use the new orientation
  145.      */
  146.  
  147.     for ( i = 0; i < NUMBER_OF_EMITTERS; i++ )
  148.         setEmitterPosition( &Em[i], Em[i].x, Em[i].y, Em[i].z );
  149.  
  150. }
  151.  
  152.  
  153.